package com.mingqi.enetconnected.primary.mytask.activity;


import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.base.BaseActivity;
import com.mingqi.enetconnected.primary.MainActivity;
import com.mingqi.enetconnected.primary.mytask.adapter.RefuseReasonAdapter;
import com.mingqi.enetconnected.primary.mytask.event.RefuseReasonEvent;
import com.mingqi.enetconnected.util.resource.ResourceUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

public class RefuseReasonActivity extends BaseActivity {


    @BindView(R.id.tv_submit)
    TextView mTvSubmit;
    @BindView(R.id.lv_reason)
    ListView mLvReason;
    @BindView(R.id.et_remark)
    EditText mEtRemark;
    @BindView(R.id.et_other_advice)
    EditText mEtOtherAdvice;
    private InputMethodManager mManager;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_refuse_reason;
    }

    @Override
    protected void initView() {

        setTitleName(ResourceUtils.getString(R.string.refuse_reason));

        mManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    @Override
    protected void initData() {

        RefuseReasonAdapter refuseReasonAdapter = new RefuseReasonAdapter();
        mLvReason.setAdapter(refuseReasonAdapter);

    }

    @Override
    protected void initClick() {

        mTvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(RefuseReasonActivity.this, "提交", Toast.LENGTH_SHORT).show();
                finish();
                EventBus.getDefault().post(new RefuseReasonEvent(true));
            }
        });

        mLvReason.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if(position == 4) {

                    boolean checked = mLvReason.isItemChecked(position);
                    if(checked) {

                        mEtOtherAdvice.setEnabled(true);
                        mEtOtherAdvice.requestFocus();
                        mManager.showSoftInput(mEtOtherAdvice,0);
                    } else {

                        mEtOtherAdvice.setEnabled(false);
                        mManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    }
                }
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (getCurrentFocus() != null
                    && getCurrentFocus().getWindowToken() != null) {
                mManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }
}
